//
//  THFlowEarnMyjoinCell.m
//  YJB
//
//  Created by 中电大维 on 2017/1/13.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "THFlowEarnMyjoinCell.h"
#import "THFlowEarnMyJoinModel.h"
#import "THFlowEarnCompleteDetaillModel.h"
NSString *const flowEarnMyJoinCellID = @"flowEarnMyJoinCellID";

@implementation THFlowEarnMyjoinCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //self.backgroundColor = kUIColorFromRGB(0xffebe7);
        //self.backgroundColor = [UIColor colorWithRed:rand()%255/255.0 green:rand()%255/255.0 blue:rand()%255/255.0 alpha:1];
        [self cpcTest];
    }
    return self;
}
- (NSMutableAttributedString *)getAttributedString:(NSString *)front Middle:(NSString *)middle Behind:(NSString *)behind{
    
    NSMutableAttributedString *attr = [NSString attributedString:front frontColor:kUIColorFromRGB(0x666666) middle:middle middleColor:kUIColorFromRGB(0xff783c) behind:behind behindColor:kUIColorFromRGB(0x666666) font:FONT(15)];
    return attr;
}

- (void)cpcTest
{
    self.iconImgView.image = Image(@"th_cpc_default_normal");
    self.titleLab.text = @"只要轻松点几下，钱就到手啦，亲还在等什么呢，赶紧点我吧...";
    self.username.text = @"18676727214";
    self.time.text = @"2016.12.25";

    //    self.award.text = @"普通奖励 3金币";
//    [self.vipAwardBtn setTitle:@"奖励" forState:UIControlStateNormal];
    
//    self.vipAward.text = @"18 金币";
//    self.surplus.text = @"已做过8/10个";
    
    self.award.attributedText = [self getAttributedString:@"奖励" Middle:@" 2 " Behind:@"金币"];
    //    self.vipAwardBtn.titleLabel.attributedText = [self getAttributedString:@"奖励" Middle:@" " Behind:@"18金币"];
    
    //self.vipAwardBtn.attributedText = [self getAttributedString:@"普通奖励" Middle:@" " Behind:@"16金币"];
    
    //    self.vipAwardBtn.backgroundColor = [UIColor orangeColor];
    //    self.vipAwardBtn.imageView.backgroundColor = [UIColor yellowColor];
    //    self.vipAwardBtn.titleLabel.backgroundColor = [UIColor redColor];
    
    
}

- (UILabel *)username{
    if (!_username) {
        _username = [[UILabel alloc] init];
        _username.textColor = [UIColor colorWithHexString:@"ff783c"];
        _username.font = FONT(15);
        [self addSubview:_username];
        [_username mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(20);
            make.left.equalTo(self.iconImgView.mas_right).offset(15);
            //make.right.equalTo(self.time.mas_right).offset(-6);
            make.height.mas_equalTo(15);
        }];
    }
    return _username;
}

-(UIImageView *)iconImgView
{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
        _iconImgView.image = Image(@"th_cpc_default_normal");
        [self addSubview:_iconImgView];
        [_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(12);
            make.left.equalTo(self.mas_left).offset(15);
            make.width.height.mas_equalTo(48);
        }];
    }
    return _iconImgView;
}

-(UILabel *)titleLab
{
    if (_titleLab == nil)
    {
        _titleLab = [[UILabel alloc]init];
        _titleLab.backgroundColor = [UIColor clearColor];
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.textColor = kUIColorFromRGB(0x333333);
        [self addSubview:_titleLab];
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.username.mas_bottom).offset(12);
            make.left.equalTo(self.username.mas_left);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(15);
        }];
    }
    return _titleLab;
}

-(UILabel *)award
{
    if (_award == nil)
    {
        _award = [[UILabel alloc]init];
        _award.backgroundColor = [UIColor clearColor];
        _award.font = [UIFont boldSystemFontOfSize:14];
        _award.numberOfLines = 0;
        _award.textColor = kUIColorFromRGB(0x666666);
        [self addSubview:_award];
        NSString *award = [NSString stringWithFormat:@"普通奖励 3金币"];
        CGFloat awardWidth = [NSString widthForString:award UIfontSize:FONT(15) andHeight:15];
        [_award mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLab.mas_bottom).mas_offset(19);
            make.left.equalTo(self.titleLab.mas_left);
            make.width.mas_equalTo(awardWidth);
            make.height.mas_equalTo(14);
        }];
    }
    
    
    return _award;
}

-(UILabel *)time
{
    if (_time == nil)
    {
        _time = [[UILabel alloc]init];
        _time.font = FONT(14);
        _time.textColor = kUIColorFromRGB(0x666666);
        CGFloat width = [NSString widthForString:@"2016.12.25" UIfontSize:FONT(14) andHeight:15];
        
        [self addSubview:_time];
        [_time mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.username.mas_right).offset(8);
            make.right.equalTo(self.mas_right).offset(-12);
            make.height.mas_equalTo(15);
            make.centerY.equalTo(self.username.mas_centerY);
            make.width.mas_equalTo(width+6);
        }];
    }
    return _time;
}
-(void)setModel:(THFlowEarnMyJoinModel *)model
{
    //    self.titleLab.text = model.sitename;
    //    //    self.award.text = award;
    //    self.award.attributedText = [self getAttributedString:@"普通奖励" Middle:@"  " Behind:[NSString stringWithFormat:@"%@ 金币",model.usergold_one]];
    //    self.vipAward.text = [NSString stringWithFormat:@"%@ 金币",model.vipgold_one];
    //
    //    [self.vipAwardBtn setTitle:@"奖励" forState:UIControlStateNormal];
    //
    //    self.surplus.text = [NSString stringWithFormat:@"已做%@/%@个",model.complete_num,model.surflimit];
    //
    //    if ([model.url rangeOfString:@"http://"].location != NSNotFound) {
    //        [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:Image(@"cpc默认icon")];
    //    }else{
    //        [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",model.url]] placeholderImage:Image(@"cpc默认icon")];
    //    }
    
    if (model.publish_name) {
        self.username.text = model.publish_name;
    }
    self.titleLab.text = model.sitename;
    self.award.attributedText = [self getAttributedString:@"奖励" Middle:[NSString stringWithFormat:@" %d ",[model.real_price intValue]] Behind:@"金币"];
    
    
    NSString *urlString = model.surf_icon;
    if (![urlString hasPrefix:@"http"]&&urlString)
        urlString = [@"http://" stringByAppendingString:urlString];
    [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:Image(@"th_cpc_default_normal")];

    self.time.text = [model.create_time stringWithDateFormat:@"yyyy.MM.dd"];
    
//    CGFloat title_height = [NSString heightForString:model.sitename fontSize:16 andWidth:TableCell_TitleWidth];
//    title_height = title_height<26?15:title_height;
//    [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(title_height);
//    }];
}

- (void)setCompleteModel:(THFlowEarnCompleteDetaillModel *)completeModel
{
    if (completeModel.username) {
        self.username.text = completeModel.username;
    }
    
    self.titleLab.text = completeModel.sitename;
    
    NSString *urlString = completeModel.image;
    if (![urlString hasPrefix:@"http"]&&urlString)
        urlString = [@"http://" stringByAppendingString:urlString];
    [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:Image(@"th_cpc_default_normal")];
    self.time.text = [completeModel.create_time stringWithDateFormat:@"yyyy.MM.dd"];
    self.award.hidden = YES;
}

@end
